package cn.stylefeng.guns.modular.basic.controller;

import java.io.File;
import java.util.List;

import cn.stylefeng.guns.core.util.QRCodeUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicPutgoods;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicPutgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicWarehouseParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicWarehouseResult;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 仓库表
 */
@Controller
@RequestMapping("/basicWarehouse")
public class BasicWarehouseController extends BaseController {

    private String PREFIX = "/basic/basicWarehouse";

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicPutgoodsService basicPutgoodsService;

    /**
     * 跳转到 选择位置
     */
    @RequestMapping("toChoiceAddress")
    public String toChoiceAddress() {
        return PREFIX + "/selectlocation.html";
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicWarehouse.html";
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("index_hx")
    public String index_hx(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicWarehouse_hx.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicWarehouse_add.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add_hx")
    public String add_hx(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicWarehouse_add_hx.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicWarehouse_edit.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit_hx")
    public String edit_hx(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicWarehouse_edit_hx.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicWarehouseParam basicWarehouseParam) {
    	Object warehouseOperateId = basicWarehouseParam.getWarehouseOperateId();
    	if (warehouseOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(warehouseOperateId.toString()));
    		basicWarehouseParam.setWarehouseOperateName(Operate.getOperateName());
		}
        
        //set 交付id
    	Long DeliverId = userService.getDeliverId();
    	if (DeliverId!=null) {
    		basicWarehouseParam.setDeliverId(DeliverId);
		}
        this.basicWarehouseService.add(basicWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicWarehouseParam basicWarehouseParam) {
    	Object warehouseOperateId = basicWarehouseParam.getWarehouseOperateId();
    	if (warehouseOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(warehouseOperateId.toString()));
    		basicWarehouseParam.setWarehouseOperateName(Operate.getOperateName());
		}
        this.basicWarehouseService.update(basicWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicWarehouseParam basicWarehouseParam) {
        this.basicWarehouseService.delete(basicWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicWarehouseParam basicWarehouseParam) {
        BasicWarehouse detail = this.basicWarehouseService.getById(basicWarehouseParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicWarehouseParam basicWarehouseParam,String condition) {
    	User user = userService.getcurrentUser();
    	basicWarehouseParam.setDeliverId(user.getDeliverId());
    	Long deptId = user.getDeptId();
        return this.basicWarehouseService.findPageBySpec(basicWarehouseParam,condition,deptId,user.getOperateId());
    }

    @Autowired
    private BasicPositionService basicPositionService;

    /**
     * 库容存量统计
     */
    @ResponseBody
    @RequestMapping("/Storagelist")
    public LayuiPageInfo Storagelist(BasicWarehouseParam basicWarehouseParam,String condition) {
    	Long deptId = userService.getDeptId();
    	List<BasicWarehouseResult> list = this.basicWarehouseService.findStoragelistPageBySpec(basicWarehouseParam,condition,deptId);
    	for (BasicWarehouseResult r : list) {
    		BasicPositionResult positionResult = basicPositionService.sumPositionStorage(Long.parseLong(r.getId().toString()));//总库位数    总可存托数
    		if(positionResult != null) {
    			r.setSumP(positionResult.getSumP());
    			if(ToolUtil.isEmpty(positionResult.getSumPt())) {
    				r.setSumPt("0");
    			} else {
    				r.setSumPt(positionResult.getSumPt());
    			}
    		} else {
    			r.setSumP("0");
    			r.setSumPt("0");
    		}
    		BasicPositionResult positionResult2 = basicPositionService.sumPositionStorageYsumP(basicWarehouseParam.getCustomerId(),Long.parseLong(r.getId().toString()));//已使用库位数
    		if(positionResult2 != null) {
    			r.setYsumP(positionResult2.getYsumP());
    		} else {
    			r.setYsumP("0");
    		}
    		BasicPositionResult positionResult3 = basicPositionService.sumPositionStorageKsumP(basicWarehouseParam.getCustomerId(),Long.parseLong(r.getId().toString()));//空库位数
    		if(positionResult3 != null) {
    			r.setKsumP(positionResult3.getKsumP());
    		} else {
    			r.setKsumP("0");
    		}
    		
    		BasicPositionResult positionResult5 = basicPositionService.sumPositionStorageYsumPt(basicWarehouseParam.getCustomerId(),Long.parseLong(r.getId().toString()));//已存托数
    		if(positionResult5 != null) {
    			r.setYsumPt(positionResult5.getYsumPt());
    		} else {
    			r.setYsumPt("0");
    		}
    		
			double a = Double.parseDouble(r.getSumPt());
			double b = Double.parseDouble(r.getYsumPt());
			r.setDsumPt(a-b+"");//总可存托数 - 已存托数   =  可存托数
		}
    	LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
    	layuiPageInfo.setData(list);
    	layuiPageInfo.setCount(list.size());
		return layuiPageInfo;
    	
    }
    @RequestMapping("/toqrCode")
    public String toqrCode() {
        return PREFIX + "/qrCode.html";
    }

    /**
     * 创建二维码
     */
    @RequestMapping(value = "/getqrcode", method = RequestMethod.GET)
    @ResponseBody
    public void generateQRCode4Product(Integer id, String name, HttpServletRequest request, HttpServletResponse response) {
        User user = userService.getcurrentUser();
        try {
            //logo
            File logoFile = new File("");
            String url = "https://jilu56it.top//signincode?warehouseId="+id;
            QRCodeUtil.drawLogoQRCode(logoFile, url, name,350,350,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 根据门店id查询仓库列表
     * @param storeId    门店id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getWarehouseByStoreId")
    public LayuiPageInfo getWarehouseByStoreId(String storeId) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        return this.basicWarehouseService.getWarehouseByStoreId(storeId,deptId,user.getOperateId());
    }
    /**
     * 新增页面仓库选择页面
     */
    @RequestMapping("/add_warehouse")
    public String add_warehouse(Model model,String storeId) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        BasicPutgoods byId = basicPutgoodsService.getById(storeId);

        model.addAttribute("storeId", storeId);
        model.addAttribute("storeName", byId.getPutgoodsName());

        //查询可选择分公司
        model.addAttribute("basicWarehouselist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));

        return PREFIX + "/index_mlc_add.html";
    }
}
